package edu.pitt.cs.zhangfan.sa.review;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import edu.pitt.cs.zhangfan.sa.model.Review;
import edu.pitt.cs.zhangfan.sa.model.ReviewCorpus;
import edu.pitt.cs.zhangfan.sa.model.ReviewDocument;
import edu.pitt.cs.zhangfan.sa.model.Reviewer;

public class ReviewReader {
	
	
	public ArrayList<ReviewCorpus> readCorpus(String path) throws IOException, CloneNotSupportedException {
		XSSFWorkbook xwb = new XSSFWorkbook(path);
		XSSFSheet sheet = xwb.getSheetAt(0);
		XSSFRow row;
		String cell;

		ArrayList<ReviewCorpus> corpus = new ArrayList<ReviewCorpus>();

		String corpusName = "";
		ReviewCorpus corp = null;
		Reviewer reviewer = null;
		ReviewDocument reviewDoc = null;

		String authorName = "";
		String reviewerName = "";
		//System.out.println(sheet.getPhysicalNumberOfRows());
		System.out.println(sheet.getLastRowNum());
		for (int i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i++) {
			row = sheet.getRow(i);
			if (row != null) {
				cell = row.getCell(row.getFirstCellNum()).toString();
				if (cell.startsWith("Social Implications")) {

				} else if (cell.startsWith("Research data")) {
					corpusName = "";
					if (corp != null) {
						if(reviewDoc!=null) corp.addReviewDoc(reviewDoc);
						corpus.add(corp);
					}
					reviewDoc = null;
					reviewer = null;
					corp = new ReviewCorpus();
				} else {
					if (!cell.trim().equals("")) { // having contents
						if (corpusName.equals("")) {
							corpusName = cell.trim();
							System.out.println(corpusName);
							corp = new ReviewCorpus();
							corp.setCorpusName(corpusName);
						} else {
							if (!cell.trim().equals("Paper Author")) {
								String currentAuthor = row.getCell(
										row.getFirstCellNum()).toString();
								String currentReviewer = row.getCell(
										row.getFirstCellNum() + 1).toString();
								String currentCommentName = row.getCell(
										row.getFirstCellNum() + 2).toString();

								String currentComment = "";
								if (row.getCell(row.getFirstCellNum() + 4) != null) {
									currentComment = row.getCell(
											row.getFirstCellNum() + 4)
											.toString();
								}
								String currentBackEvalComment = "";
								if (row.getCell(row.getFirstCellNum() + 5) != null) {
									currentBackEvalComment = row.getCell(
											row.getFirstCellNum() + 5)
											.toString();
								}
								int currentBackEvalScore = -1;
								if (row.getCell(row.getFirstCellNum() + 6) != null) {
									currentBackEvalScore = (int) Float
											.parseFloat(row.getCell(
													row.getFirstCellNum() + 6)
													.toString());
								}

								Review review = new Review();
								review.setAuthor(currentAuthor);
								review.setDocumentName(corpusName);
								review.setReviewer(currentReviewer);
								review.setComment(currentComment);
								review.setShortCommentName(currentCommentName);
								review.setBackEvalComment(currentBackEvalComment);
								review.setBackEvalScore(currentBackEvalScore);
								
								if(!currentAuthor.equals(authorName)) {
									//Changing author
									if(reviewDoc!=null) {
										if(reviewer!=null) reviewDoc.addReviewer(reviewer);
										corp.addReviewDoc(reviewDoc);//add old
										authorName = currentAuthor;
										reviewDoc = new ReviewDocument(); //new review document
										reviewDoc.setAuthor(authorName);
										reviewer = new Reviewer();
										reviewerName = currentReviewer;
										reviewer.setReviewerName(reviewerName);
										reviewer.addReview(review);
									} else {
										//new data, there is no author
										reviewDoc = new ReviewDocument();
										authorName = currentAuthor;
										reviewDoc.setAuthor(authorName);
										reviewer = new Reviewer();
										reviewerName = currentReviewer;
										reviewer.setReviewerName(reviewerName);
										reviewer.addReview(review);
										
									}
								} else {
									if(!currentReviewer.equals(reviewerName)) {
										//Changing reviewer
										reviewDoc.addReviewer(reviewer);
										reviewer = new Reviewer();
										reviewerName = currentReviewer;
										reviewer.setReviewerName(reviewerName);
										reviewer.addReview(review);
									} else {
										reviewer.addReview(review);
									}
								}
								
							}
						}
					}
				}
			}
		}
		corpus.add(corp);
		return corpus;
	}
	
	public void writeReviews(ArrayList<ReviewCorpus> rc,String path) throws IOException {
		for(int i = 0;i<rc.size();i++) {
			ReviewCorpus corpus = rc.get(i);
			String folderPath = path + "/" + i;
			ArrayList<ReviewDocument> docs = corpus.getDocs();
			for(int j = 0;j<docs.size();j++) {
				ReviewDocument doc = docs.get(j);
				String fileName = folderPath + "/" + doc.getAuthor()+".txt";
				File f = new File(fileName);
				if(!f.exists()) f.getParentFile().mkdirs();
				ArrayList<Reviewer> reviewers = doc.getReviewers();
				BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
				for(int k = 0;k<reviewers.size();k++) {
					Reviewer rv = reviewers.get(k);
					ArrayList<Review> rvs = rv.getReviews();
					for(int l = 0;l<rvs.size();l++) {
						String content = rv.getReviewerName()+"-"+l+"\t"+rvs.get(l).getComment();
						writer.write(content+"\n");
					}
				}
				writer.close();
			}
		}
	}

	public static void main(String[] args) throws IOException, CloneNotSupportedException {
		ReviewReader rr = new ReviewReader();
		ArrayList<ReviewCorpus> rc = rr
				.readCorpus("E:/independent study/sentence alignment/peerComments.xlsx");
		rr.writeReviews(rc,"E:/independent study/sentence alignment/comments");
		for (int i = 0; i < rc.size(); i++) {
			System.out.println(rc.get(i).toString());
		}
	}
}
